<?php

namespace app\common\order\model;

use app\common\config\logic\CommonPickerLogic;
use app\common\system\logic\UserLogic;
use think\Model;
use think\model\concern\SoftDelete;

class OrderIncome extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $type = [
        'income_date' => 'timestamp',
        'check_time' => 'timestamp',
    ];

    public function order()
    {
        return $this->hasOne(Order::class, 'id', 'order_id');
    }

    public function vip()
    {
        return $this->hasOne(OrderVip::class, 'id', 'vip_id');
    }

    public function bom()
    {
        return $this->hasOne(OrderBom::class, 'id', 'bom_id');
    }


    public function getReceiptAttr($value, $data)
    {
        return empty($value) || $value == 'null' ? [] : json_decode($value, true);
    }

    public function setReceiptAttr($value)
    {
        return empty($value) ? '[]' : json_encode($value);
    }

    public function getNoteAttr($value, $data)
    {
        return empty($value) || $value == 'null' ? [] : json_decode($value, true);
    }

    public function setNoteAttr($value)
    {
        return empty($value) ? '[]' : json_encode($value);
    }

    public function getWayAttr($value, $data)
    {
        $commonLogic = new CommonPickerLogic();
        return $commonLogic->getPickerItem('paidWay', $data['income_way_id']);
    }

    public function getTypeAttr($value, $data)
    {
        $commonLogic = new CommonPickerLogic();
        return $commonLogic->getPickerItem('paidType', $data['income_type_id']);
    }

    public function getUserAttr($value, $data)
    {
        $userLogic = new UserLogic();
        return $userLogic->getUser($data['user_id'], ['id', 'realname', 'status']);
    }

    public function getIncomeDateAttr($value, $data)
    {
        return date('Y-m-d', $value);
    }
}
